const mongoose = require('./db');


const UserDeviceModel = mongoose.model('UserDevice', new mongoose.Schema({
    username: {
        type: String,
        index: true,
        required: true
    },
    deviceId: {
        type: String,
        required: true,
    },
    deviceName: String,
    deviceType: {
        type: String,
        default: 0
    },        //设备类型 0：默认 1：NB-IOT 2:开关
    sn: String,
    imei: String,
    protocal: String,      //通信协议 0：Modbus TCP 1：数据透传
    address: String,     //江西省南昌市江西理工大学
    location: String,   //经度：115.89352755,纬度：28.689578
    modifyTime: {
        type: String,
    },
    //从机设备不分开存储
    deviceSlaver: [{
        slaverIndex: String,
        slaverName: String,
        frequency: {            //采集频率
            type: String,
            default: 0
        },
        templateId: String,
        templateName: String
    }]
}), 'userDevices')



const DataTemplateModel = mongoose.model('DataTemplate', new mongoose.Schema({
    username: {
        type: String,
        index: true
    },
    templateId: String,
    children:[{
            dataName: String,
            dataId: String,
            dataType: String,//0：数值  1：开关 
            valueKind: String,  //0：两字节无符号整数 1：两字节带符号整数
            plc: String, //PLC组态地址
            formula: Array,
            storage: Boolean  //是否保存  
        }]
}), 'dataTemplates')


const DeviceDataModel = mongoose.model('DeviceData', new mongoose.Schema({
    deviceId: {
        type: String,
        index: true,
    },
    slaverIndex: String,
    value: String,
    createTime: {
        type: String,
        default: Date.now(),
    },
}), 'deviceDatas')


const SerialPortModel = mongoose.model('SeriaPort',new mongoose.Schema({
    com:String,
    slaverIndex:String,
    value:String,
    createTime:{
        type:String,
        default:Date.now()
    }

}),'seriaPorts')


module.exports = { DeviceDataModel, DataTemplateModel,UserDeviceModel ,SerialPortModel}